package cn.com.backstage.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_advert;
import cn.com.api.entity.ApiU_bonus;
import cn.com.api.service.ApiU_advertService;
import cn.com.api.service.ApiU_bonusService;

import cn.com.util.*;

public class U_bonusBaseController {

	private static Logger logger = Logger.getLogger(U_bonusBaseController.class);

	@Autowired
	private ApiU_bonusService apiU_bonusService;
	@Autowired
	private ApiU_advertService apiU_advertService;

	/**
	 * 红包列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_bonus-list")
	public ModelAndView u_bonus_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_bonus_list.jsp");
		String packettype = request.getParameter("packettype");
		Map<String, Object> map = new HashMap<String, Object>();
		if (packettype == null || "".equals(packettype)) {
			map.put("position_ids", "3,8,17");
		} else if ("hanbonus".equals(packettype)) {
			map.put("position_ids", "17");
		} else if ("allpingpacket".equals(packettype) || "recommendpacket".equals(packettype)
				|| "callpacket".equals(packettype)) {
			map.put("packettype", packettype);
		}

		// if (packettype != null && !"".equals(packettype)) {
		// mv.addObject("packettype", packettype);
		// map.put("packettype", packettype);
		// }
		List<ApiU_bonus> list = apiU_bonusService.findBonusByMap(map);
		mv.addObject("u_bonuss", list);
		return mv;
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_bonus-delete")
	public void u_bonus_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// delete
		int ret_i = apiU_bonusService.deleteU_bonus(Integer.valueOf(id));

		map.put("bonus_id", id);
		ApiU_advert advert = apiU_advertService.findAdvertsInfoByMap(map);
		map.clear();
		apiU_advertService.deleteU_advert(advert.getId());
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
